package com.polaris.live.viewmodel

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.manager.startCountdown
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.net.homeService
import com.polaris.live.net.liveService
import com.polaris.live.net.payerService
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.BannerBean
import com.polaris.live.resp.back_resp.PartyGameCoinExchangePriceResp
import com.polaris.live.resp.back_resp.RechargeCoinPurchasePriceResp
import com.polaris.live.resp.back_resp.RechargePriceResp
import com.polaris.live.resp.go_resp.GameCoinBean
import com.polaris.live.translation.R.string
import kotlinx.coroutines.Job

/**
 * RechargeViewModel
 *
 * @author Created by 半仙 on 2023/10/16/016 21:03
 */
class RechargeViewModel : BaseViewModel() {

    val rechargeLiveData = MutableLiveData<ResultState<RechargePriceResp>>()

    val bannerLiveData = MutableLiveData<ResultState<List<BannerBean>>>()

    val gameCoinLiveData = MutableLiveData<ResultState<PartyGameCoinExchangePriceResp>>()

    val timeChange = MutableLiveData<Long>()

    private var timeJob: Job? = null

    fun getRechargeList() {
        request({ payerService.rechargePrice() }, rechargeLiveData)
    }

    fun getBannerList() {
        request({ homeService.getBannerList(CommonConst.BannerType.BANNER_PAY) }, bannerLiveData)
    }

    fun startTimeCount(rewardsExp: Long) {
        timeJob?.cancel()
        timeJob = viewModelScope.startCountdown(rewardsExp, 1000L, {
            timeChange.value = it
        }) {
            if (it <= 0) {
                timeChange.value = it
                getRechargeList()
            }
        }
    }

    fun getGameCoin() {
        requestLoading({ liveService.getPartyGameCoin() }, gameCoinLiveData, waitTime = 0)
    }

    fun gameCoinExchange(coin: Long, context: Context) {
        requestLoading(
            { liveService.gameCoinExchange(GameCoinBean(coin)) }, {
                ToastGravity.showGravity(StringUtils.getString(string.game_coin_success))
            }, {
                if (it.errCode == CommonConst.ErrorCode.SENDGIFT) {
                    GlobalPopupManager.showRechargePopup(context, this, isNeedBottom = true)
                }
                ToastGravity.showGravity(it.errorMsg)
            },
            waitTime = 0
        )
    }
    val purchasePriceLiveData = MutableLiveData<ResultState<RechargeCoinPurchasePriceResp>>()
    fun purchasePrice() {
        requestLoading({ payerService.purchasePrice("payerService") }, purchasePriceLiveData)
    }
}